#coding:utf-8
import sys
import os
from configobj import ConfigObj,ConfigObjError


def makeHomeDir(homeDir):
        print "make",os.path.isdir(homeDir)
        if not os.path.isdir(homeDir):
                os.mkdir(homeDir)
                os.mkdir(os.path.join(homeDir,"tmp"))
                os.mkdir(os.path.join(homeDir,"history"))
                os.mkdir(os.path.join(homeDir,"cache"))
                os.chmod(homeDir,0700)
        if not os.path.isdir(os.path.join(homeDir,"tmp")):
                os.mkdir(os.path.join(homeDir,"tmp"))
        if not os.path.isdir(os.path.join(homeDir,"cache")):
                os.mkdir(os.path.join(homeDir,"cache"))

def loadConfig(main):
        config = {"jid":"",
                "password":"",
                "port":5222,
                "priority":0,
                "autoPriority":"False",
                "notifyView":"True"}
        rewrite = False
        if not os.path.isdir(main.homeDir):
                makeHomeDir(main.homeDir)
                rewrite = True
        try:
                main.config = ConfigObj(os.path.join(main.homeDir,"config.ini"), encoding='UTF8')
        except ConfigObjError,e:
                main.config = e.config
        except UnicodeDecodeError,e:
                main.config = ConfigObj(encoding="UTF8")
                main.config.filename = os.path.join(main.homeDir,"config.ini")
                rewrite = True
        for key,var in config.iteritems():
                if not main.config.has_key(key):
                        main.config[key] = var
                        rewrite =  True
        if rewrite:
                main.config.write()
        
                
        
                

def getHomeDir():
        for x in range(0,len(sys.argv)):
                if sys.argv[x] == '--home':
                        return(sys.argv[x+1]);
        if sys.platform != 'win32' :
                return unicode(os.path.expanduser( '~' )+'/.AskSeeLove')
        def valid(path):
                if path and os.path.isdir(path):
                        return True
                return False
        def env(name):
                return os.environ.get( name, '' )
        homeDir = env( 'APPDATA' )
        if not valid(homeDir):
                homeDir = env( 'HOME' )
                if not valid(homeDir):
                        homeDir = '{0}{1}'.format(env('HOMEDRIVE'),env('HOMEPATH'))
                        if not valid(homeDir):
                                homeDir = env( 'SYSTEMDRIVE' )
                                if homeDir and (not homeDir.endswith('\\')):
                                        homeDir += '\\'
                                if not valid(homeDir):
                                        homeDir = 'C:\\'
        homeDir = homeDir + '\AskSeeLove'
        return unicode(homeDir, sys.getfilesystemencoding())
        
def getProfiles(homedir):
        makeHomeDir(homedir)
        return filter(lambda file : os.path.isdir(os.path.join(homedir,unicode(file))) and file.endswith("-profile"),os.listdir(homedir))

                
